#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    string addStrings(string num1, string num2) {
        int end1 = num1.size() - 1;
        int end2 = num2.size() - 1;
        string retstr;

        retstr.reserve(num1.size() > num2.size() ? num1.size() + 1 : num2.size() + 1);

        int next = 0;
        while (end1 >= 0 || end2 >= 0) {
            int val1 = end1 >= 0 ? num1[end1--] - '0' : 0;
            int val2 = end2 >= 0 ? num2[end2--] - '0' : 0;

            int ret = val1 + val2 + next;
            next = ret / 10;
            ret = ret % 10;

            retstr += ('0' + ret);
        }
        if (next == 1) {
            retstr += '1';
        }

        reverse(retstr.begin(), retstr.end());
        return retstr;

    }

};